package cn.org.tpeach.nosql.framework;

import cn.org.tpeach.nosql.enums.LogType;
import cn.org.tpeach.nosql.enums.MsgType;
import cn.org.tpeach.nosql.tools.DateTools;
import cn.org.tpeach.nosql.tools.StringUtils;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@Builder
public class LogMsg {
    private LocalDateTime receiveTime;
    private String server;
    private String msg;
    private LogType logType;
    private MsgType msgType;


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if(receiveTime != null){
            sb.append(DateTools.format(receiveTime,DateTools.DATE_TIMES));
            sb.append(" ");
        }
        if(StringUtils.isNotBlank(server)){
            sb.append("[Server ");
            sb.append(server);
            sb.append("] ");
        }
        if(MsgType.REQUEST.equals(msgType)){
            sb.append(" > [Send Command] :");
        }else if(MsgType.RESPONSE.equals(msgType)){
            sb.append(" < [Response Received] :");
        }
        if(StringUtils.isNotEmpty(msg)){
            if( msg.length()>10*1024){
                sb.append(msg.substring(0,10*1024)+"......(ignore:"+(msg.length()-10*1024)+")");
            }else{
                sb.append(msg);
            }
        }
        return sb.toString();
    }
}